Yassl: Scripting language Extending yassl
All objects in the yassl world are subclassed from the base pnode class in the yassl package. New functions are created by subclassing the javaproc class. The interpreter dynamically loads these classes when it encounters references to them. It uses a mangled version of the name to find the class. The mangling works like this: a function Xyz in yassl is implemented in the Java class yasslProcXyz. So to implement a function sqrt, we create a class yasslProcsqrt.
import yassl.*;
public class yasslProcsqrt extends javaproc
{
public pnode apply(pnode args[])
throws yasslError
{
if (args.length != 1)
{
throw new yasslError("sqrt expects 1 argument", this);
}
pnode p = args[0];
if (!(p instanceof numnode))
{
throw new yasslError("sqrt expects its argument to be a number", this);
}
double val = ((numnode)p).val;
val = Math.sqrt(val);
return new numnode(val);
}
}
All yassl objects are subclassed from pnode. There are a few primitive
types like numnode,
stringnode,
booleannode and so on.
sqrt expects to find and return a number, so there is no need
to implement a new type. If you have to introduce a new type into the system, you can choose to wrap your object using primnode, which stores an arbitrary object.
Place the .class file along with wherever you have placed the rest of the yassl support classes.
Check it out
This class is already sitting on this web site.
Go over to the
interpreter and try things like sqrt(10);, sqrt(-1);,
sqrt("some bad parameter"); and so on.
KB Sriram
Comments, bug reports: kbs@sbktech.org
Revised: Sat May 25 20:56:12 1996
URL: http://www.sbktech.org/yas_mkf.html